#ifndef FILEUTIL
#define FILEUTIL

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <vector>
#include <fstream>
#include <sstream>
#include <filesystem>

// typedef ;
namespace bucai
{

    namespace fs = std::filesystem;
    class fileUtil
    {
    private:
        std::string _filename;

        struct stat _stat; // 系统文件接口 Linux和win平台都有内置

    public:
        fileUtil(const std::string &name) : _filename(name)
        {
            stat(_filename.c_str(), &_stat);
        }
        size_t FileSize()
        {
            // return fs::file_size(_p1);
            return _stat.st_size;
        }
        time_t LastATime()
        {
            return _stat.st_atime; // 系统文件接口 Linux和win平台都有内置
        }
        time_t LastMTime()
        {
             //return fs::last_write_time(_p1); //C++17文件类


            time_t mtime= _stat.st_mtime;
            return mtime;
        }
        std::string FileName()
        {

            /*size_t num = _filename.find_last_of("/");
            std::string name = _filename.substr(num + 1);
            return name;*/
            return fs::path(_filename).filename().string();
        }
        bool GetPosLen(std::string *content, size_t pos, size_t len)
        { // 读取文件pos位置，len长度
            std::ifstream ifs;
            ifs.open(_filename, std::ios::binary);
            ifs.seekg(pos, std::ios::beg);

            if (FileSize() == 0)
            {
                std::cout << "file not exist" << std::endl;
                ifs.close();
                return false;
            }

            content->resize(len);
            ifs.read(&(*content)[0], len);

            ifs.close();
            return true;
        }
        bool GetContent(std::string *content)
        { // 读取文件全部内容
            GetPosLen(content, 0, FileSize());
            return true;
        }
        bool SetContent(const std::string &content)
        { // 写入文件
            std::ofstream ofs;
            ofs.open(_filename, std::ios::binary); // 是覆盖
            
            std::cout << "body is " << content << std::endl;

            ofs.write(&content[0], content.size());
            ofs.close();
            return true;
        }
        
        
        bool Exists()
        {
            return fs::exists(_filename);
        }
        bool createDirectory()
        {
            if (this->Exists())
                return true;
            return fs::create_directory(_filename);
        }
        bool ScanDirectory(std::vector<std::string> *arry)
        {
            for (auto &p : fs::directory_iterator(_filename))
            {
                if (fs::is_directory(p) == true)
                {
                    continue;
                }
                // relative_path 带有路径的文件名
                //arry->push_back(fs::path(p).relative_path().string());
                arry->push_back(fs::path(p).string());
            }
            return true;
        }
        bool Remove()
        {
            if (this->Exists() == false)
            {
                return true;
            }
            remove(_filename.c_str());
            return true;
        }
    };
}; // namespace bucai

#endif